//
//  HUDHelper.m
// 
//
//

#import <SharedInstanceGCD/SharedInstanceGCD.h>
#import "HUDHelper.h"
#import "CustomizedProgressHUD.h"

@implementation HUDHelper

SHARED_INSTANCE_GCD;

- (void)showHUD
{
    return [self showHUDWithText:NSLocalizedString(@"Connecting...", nil)];
}

- (void)showHUDWithText:(NSString *)labelText
{
    UIView *view = [[UIApplication sharedApplication].delegate window];
    DDLog(@"showHUDWithText(%@) - before count is %u", labelText, (unsigned int)[[CustomizedProgressHUD allHUDsForView:view] count]);
    CustomizedProgressHUD *hud = [CustomizedProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc]
                                              initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    [indicatorView startAnimating];
    hud.customView = indicatorView;
    hud.minSize = CGSizeMake(80, 80);
    hud.labelText = labelText;
    hud.userInteractionEnabled = YES;
}

- (void)hideHUD
{
    UIView *view = [[UIApplication sharedApplication].delegate window];
    DDLog(@"hideHUD - before count is %u", (unsigned int)[[CustomizedProgressHUD allHUDsForView:view] count]);
    // 互換性のため残す
    if ([[UIApplication sharedApplication] isIgnoringInteractionEvents]) {
        [[UIApplication sharedApplication] endIgnoringInteractionEvents];
    }
    [CustomizedProgressHUD hideAllHUDsForView:view animated:YES];
}

- (BOOL)isShownHUD
{
    UIView *view = [[UIApplication sharedApplication].delegate window];
    NSArray *HUDs = [CustomizedProgressHUD allHUDsForView:view];
    DDLog(@"isShownHUD - current count is %u", (unsigned int)[HUDs count]);
    return [HUDs count] == 0 ? NO : YES;
}


- (void)showToastWithMessage:(NSString *)message
{
    [self showToastWithMessage:message yOffset:45];
}

- (void)showToastWithMessage:(NSString *)message yOffset:(CGFloat)offset
{
    [self showToastWithTitle:nil message:message yOffset:offset];
}

- (void)showToastWithTitle:(NSString *)title message:(NSString *)message yBottomOffset:(CGFloat)offset
{
    UIView *view = [[UIApplication sharedApplication].delegate window];
    CustomizedProgressHUD *hud = [CustomizedProgressHUD showHUDAddedTo:view animated:YES];
    hud.labelText = title;
    hud.detailsLabelText = message;
    hud.mode = MBProgressHUDModeText;
    hud.opacity = 0.6;
    hud.minSize = CGSizeMake(300, 44);
    
    CGFloat toastBottomMargin = offset;
    CGFloat viewHeight = CGRectGetHeight(view.frame);
    CGFloat yPos0 = roundf((viewHeight - hud.minSize.height) / 2);
    CGFloat yPos  = viewHeight - toastBottomMargin - hud.minSize.height;
    hud.yOffset = yPos - yPos0;
    
    hud.userInteractionEnabled = NO;
    [hud hide:YES afterDelay:3.0];
}

- (void)showToastWithTitle:(NSString *)title message:(NSString *)message yOffset:(CGFloat)offset
{
    UIView *view = [[UIApplication sharedApplication].delegate window];
    CustomizedProgressHUD *hud = [CustomizedProgressHUD showHUDAddedTo:view animated:YES];
    hud.labelText = title;
    hud.detailsLabelText = message;
    hud.mode = MBProgressHUDModeText;
    hud.opacity = 0.6;
    hud.minSize = CGSizeMake(300, 44);
    CGFloat viewHeight = CGRectGetHeight(view.frame);
    CGFloat yPos0 = roundf((viewHeight - hud.minSize.height) / 2);
    hud.yOffset = offset-yPos0;
    
    hud.userInteractionEnabled = NO;
    [hud hide:YES afterDelay:3.0];
}

- (void)showToastAlways:(NSString *)title message:(NSString *)message yOffset:(CGFloat)offset
{
    UIView *view = [[UIApplication sharedApplication].delegate window];
    CustomizedProgressHUD *hud = [CustomizedProgressHUD showHUDAddedTo:view animated:YES];
    hud.labelText = title;
    hud.detailsLabelText = message;
    hud.labelColor = [UIColor yellowColor];
    hud.detailsLabelColor = [UIColor yellowColor];
    hud.mode = MBProgressHUDModeText;
    hud.opacity = 0.6;
    hud.minSize = CGSizeMake(300, 44);
    CGFloat viewHeight = CGRectGetHeight(view.frame);
    CGFloat yPos0 = roundf((viewHeight - hud.minSize.height) / 2);
    hud.yOffset = offset-yPos0;
    
    hud.userInteractionEnabled = NO;
}

@end
